package com.csaba.connector.axa;

import java.util.Currency;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.csaba.connector.I18NServiceException;
import com.csaba.connector.ServiceException;
import com.csaba.connector.axa.model.AXAAccount;
import com.csaba.connector.axa.model.AXABank;
import com.csaba.connector.axa.model.AXASession;
import com.csaba.connector.model.Account;
import com.csaba.connector.model.Customer;
import com.csaba.connector.model.Session;
import com.csaba.connector.service.AbstractBankService;

public class AXASMSOTPValidationService extends AbstractBankService implements Codes
{
	private String smsotp;
	private Account[] accounts;

	@Override
	public void execute( final Session session ) throws ServiceException
	{
		if ( !( session instanceof AXASession ) )
			throw new ServiceException("Incompatible session " + AXABank.class + " != " + session.getClass().getName());
		if ( smsotp == null )
			throw new ServiceException("Service is not yet inicialized.");

		final AXASession axa = (AXASession) session;

		final NameValuePair[] otpData = new NameValuePair[] {// 
		new BasicNameValuePair("OTP", smsotp),//
		};

		final String otpResult = axa.doPost(AXASession.getRequestURL(REQUEST_VERIFY_OTP), otpData);
		axa.debugFile("otpResult", otpResult);

		AXAUtil.checkForHTMLError(otpResult, "err.otpCheckFailed");

		//CLIENTINFO
		final NameValuePair[] clientInfoData = new NameValuePair[] {// 
		new BasicNameValuePair("AccountNumber", ""),//
		};

		final String clientInfoResult = axa.doPost(AXASession.getRequestURL(REQUEST_GET_CUSTOMER_INFO), clientInfoData);
		axa.debugFile("clientInfoResult", clientInfoResult);

		String clientId = "";
		try
		{
			//result: ["Customer Name","ClientID"]
			final JSONArray array = new JSONArray(clientInfoResult);
			final String customerName = array.getString(0);
			clientId = array.getString(1);

			final Customer customer = axa.getCustomer();
			customer.setName(customerName);
			customer.setId(clientId);
		}
		catch ( final JSONException e )
		{
			throw new I18NServiceException(AXABank.getInstance(), "err.getCustomerFailed", e);
		}

		//GET ACCOUNTS
		/*
		 * ClientID=xxxxxxxx
		 * Locale=hun/enu
		 * AccountNumber=
		 */
		final NameValuePair[] accountRequestData = new NameValuePair[] {// 
		new BasicNameValuePair("ClientID", clientId),//
				new BasicNameValuePair("Locale", (String) axa.getRemoteProperty(RP_LANGUAGE)),//
				new BasicNameValuePair("AccountNumber", ""),//
		};

		final String accountsResult = axa.doPost(AXASession.getRequestURL(REQUEST_GET_ACCOUNTS), accountRequestData);
		axa.debugFile("getAccountsResult", accountsResult);

		try
		{
			//store accounts for further processing
			/*
			 * [
			 * 	{
			 * 		"__id":0,
			 * 	"GIRONameFrom":"aaa bbb",
			 * 		"Company":"H1    ",
			 * 	"AccountNumber":"xxxxxxxx",
			 * 	"OwnerID":"xxxxxxxx",
			 * 	"PIN":null,
			 * 	"Currency":"HUF ",
			 * 		"JoinedAccount":"SA",
			 * 		"ProductType":"SAIN",
			 * 		"BranchID":"17000019",
			 * 		"FeeSet":"SAIN",
			 * 	"Description":"Private Account",
			 * 		"RequestDate":null,
			 * 		"Enabled":"9",
			 * 		"OwnerStatus":"1"
			 * 	},
			 * ]
			 */
			final JSONArray array = new JSONArray(accountsResult);

			accounts = new Account[array.length()];
			for ( int i = 0; i < array.length(); i++ )
			{
				final JSONObject object = array.getJSONObject(i);
				final Account account = new AXAAccount();
				account.setBank(AXABank.getInstance());
				account.setCurrency(Currency.getInstance(object.getString("Currency").trim()));
				account.setCustomer(axa.getCustomer());
				account.setName(object.getString("GIRONameFrom"));
				account.setNumber(object.getString("AccountNumber"));
				account.setType(object.getString("Description"));

				AXAUtil.setStringProperty(account, object, "Company");
				AXAUtil.setStringProperty(account, object, "JoinedAccount");
				AXAUtil.setStringProperty(account, object, "ProductType");
				AXAUtil.setStringProperty(account, object, "BranchID");
				AXAUtil.setStringProperty(account, object, "FeeSet");
				AXAUtil.setStringProperty(account, object, "Enabled");
				AXAUtil.setStringProperty(account, object, "OwnerStatus");

				accounts[i] = account;
			}

			axa.setRemoteProperty(RP_ACCOUNT_LIST, accounts);
		}
		catch ( final JSONException e )
		{
			throw new I18NServiceException(AXABank.getInstance(), "err.accountListFailed", e);
		}

	}

	public void setSmsotp( final String smsotp )
	{
		this.smsotp = smsotp;
	}

	public Account[] getAccounts()
	{
		return accounts;
	}

}
